package com.web.controller.lxf;

import com.core.dto.PageDTO;
import com.core.dto.UserDTO;
import com.core.model.user.Administrator;
import com.service.user.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 2016-01-08
 * Time: 11:11
 * To change this template use Editor | File and Code Templates.
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdministratorService administratorService;

    @RequestMapping(value = "manager", method = RequestMethod.GET)
    public String adminManager() {
        return "back/user/admin";
    }

    @RequestMapping(value = "list/{currentPage}", method = RequestMethod.GET)
    @ResponseBody
    public Object adminList(@PathVariable String currentPage, Model model) {
        return administratorService.findAllAdminUser(currentPage);
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Object addAdminUser(UserDTO userDTO) {
        return administratorService.addAdminUser(userDTO);
    }

    @RequestMapping(value = "del/{adminUserGuid}", method = RequestMethod.GET)
    @ResponseBody
    public Object delAdminUser(@PathVariable String adminUserGuid) {
        return administratorService.delAdminUser(adminUserGuid);
    }

    @RequestMapping(value = "reset_pwd", method = RequestMethod.POST)
    @ResponseBody
    public Object resetAdminPassword(String adminUserGuid, String newPassword) {
        return administratorService.resetAdminPwd(adminUserGuid, newPassword);
    }

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public ModelAndView login() {
        return new ModelAndView("back/login", "userDTO", new UserDTO());
    }

    @RequestMapping(value = "find/{username}")
    @ResponseBody
    public Object findAdmin(@PathVariable String username) {
        Administrator administrator = administratorService.findAdminByUsername(username);
        return administrator;
    }
}
